/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Controller.java
 * 
 * The controller class for the plugin
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation 
 *    Sangeeth Nambiar - initial implementation
 *******************************************************************************/
package edu.cmu.isr.dinvenio;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewPart;

import edu.cmu.isr.dinvenio.dataConnector.DepsXMLFileConnector;
import edu.cmu.isr.dinvenio.model.DepsGraph;
import edu.cmu.isr.dinvenio.ui.graphViewer.JungGraph;
import edu.cmu.isr.dinvenio.ui.graphViewer.JungViewer;
import edu.cmu.isr.dinvenio.ui.views.LayersViewPart;
import edu.cmu.isr.dinvenio.ui.views.LayoutViewPart;
import edu.cmu.isr.dinvenio.ui.views.ProjectViewPart;
import edu.cmu.isr.dinvenio.ui.views.SettingsViewPart;

public class Controller {

	private DepsGraph graph;
	private JungGraph jungGraph;
	private JungViewer viewer;
	/**
	 * 
	 */
	public Controller() {
		this.graph = new DepsGraph();
	}
	public DepsGraph getGraphModel() {
		return graph;
	}
	public void loadDataToModel(Shell shell) {
		 DepsXMLFileConnector fileConnector = new DepsXMLFileConnector();
		 fileConnector.loadDataToModel(shell,this.graph);
		 Console.printToConsole("Data loaded");
		 
		 this.jungGraph = new JungGraph(this.graph);
		 if (this.viewer != null)
		 {
			 this.viewer.disposeWidgets();
		 }
		 this.viewer = new JungViewer(jungGraph);
		 
		 IViewPart myviewPartr = Activator.getSettingsView();
		 ((SettingsViewPart)myviewPartr).addNodeProperties(graph.getAvailableNodeProperties());
		
		 myviewPartr = Activator.getLayoutView();
		 ((LayoutViewPart)myviewPartr).addLayoutNames(viewer.getAvailableLayouts(), viewer.getDefaultLayout());
		
		 myviewPartr = Activator.getLayersView();
		 ((LayersViewPart)myviewPartr).addLayerIDs(graph.getLayerIDs());
		 
		 myviewPartr = Activator.getProjectView();
		 ((ProjectViewPart)myviewPartr).refreshView();
	}

	public void drawGraph(Shell shell) {
		if (this.jungGraph != null){
			this.jungGraph.transtlateDepsGraphToJung();
			viewer.refresh();
		}
		else {
			this.loadDataToModel(shell);
			this.drawGraph(shell);
		}
	}

	public void reDrawGraph(Shell shell) {
		clearGraph();
		drawGraph(shell);	
	}

	public void clearGraph() {
		this.jungGraph.clearGraph();		
	}

	public void refreshGraphNodes() {
		this.viewer.refreshNodes();		
	}

	public void setLayerVisibility(String layerID, boolean visibility) {
		this.jungGraph.setLayerVisibility(layerID, visibility);		
	}

	public void disableLayer(String layerID, Shell shell) {
		this.jungGraph.disableLayer(layerID);		
	}

	public void enableLayer(String layerID, Shell shell) {
		this.jungGraph.enableLayer(layerID);		
	}

	public void setNodeColorStyleRG(boolean b) {
		if(this.viewer != null){ this.viewer.setNodeColorStyleRG(b);}		
	}

	public void setNodeColorProperty(String p) {
		this.graph.setPropertyForNodeColor(p);
	}

	public void setNodeSizeProperty(String p) {
		this.graph.setPropertyForNodeSize(p);
	}

	public void refreshGraphSize() {
		if (this.viewer != null){
			this.viewer.refreshGraphSize();
		}			
	}
	public Color getLayerColor(String layer, Display display) {
		return this.graph.getLayer(layer).getSWTColor(display);
	}
	public void setGraphLayout(String layout) {
		viewer.setLayout(layout);
		
	}
}
